﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.OleDb;
using System.Configuration;
using System.Drawing;

namespace WeakEndForum
{
    public partial class ProfilePage : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {

            if (Modules.isUserConnected())
            {
                if (Session["changeAvatarSuccess"] != null)
                {
                    literProfileSuccess.Text = "<p class=\"alert alert-success\">Votre avatar a été changé avec succès</p>";
                    Session["changeAvatarSuccess"] = null;
                }
                OleDbConnection connection = new OleDbConnection(ConfigurationManager.ConnectionStrings[Constantes.DB_NAME].ConnectionString);

                OleDbCommand getUserInfos = new OleDbCommand("SELECT Username, Email, Image_Avatar FROM Utilisateurs WHERE Username = @username;", connection);
                getUserInfos.Parameters.Add(new OleDbParameter("username", Modules.getConnectedUser()) { OleDbType = OleDbType.VarChar, Size = 255 });

                connection.Open();
                OleDbDataReader dataReader = getUserInfos.ExecuteReader();

                if (dataReader.Read())
                {
                    lblUserName.Text = (string)dataReader[0];
                    lblEmail.Text = (string)dataReader[1];
                    imgAvatar.ImageUrl = @"User_Data\images\" + (string)dataReader[2];
                    lblStatut.Text = statutUser();
                }
                connection.Close();
            }
            else
            {
                Response.Redirect("Error401.aspx");
            }
        }

        protected void btnChanger_Image_Click(object sender, EventArgs e)
        {
            string errorMessages = "";
            bool ValidImage = false;
            if (fuAvatar.PostedFile.ContentLength > 0)
            {
                if (fuAvatar.PostedFile.ContentType == "image/jpeg" || fuAvatar.PostedFile.ContentType == "image/jpg" || fuAvatar.PostedFile.ContentType == "image/png" || fuAvatar.PostedFile.ContentType == "image/gif")
                {
                    Bitmap tempImg = new Bitmap(fuAvatar.PostedFile.InputStream);
                    if (tempImg.Height <= 180 && tempImg.Width <= 180)
                    {
                        ValidImage = true;
                    }
                    else
                    {
                        errorMessages = ("L'image spécifiée est trop grande");
                    }
                }
                else
                {
                    errorMessages = ("Le fichier spécifié n'est pas une image");
                }
            }
            else
            {
                errorMessages = ("Aucun fichier n'a été spécifié");
            }

            if (ValidImage)
            {
                string ImageName = fuAvatar.PostedFile.FileName;
                OleDbConnection connection = new OleDbConnection(ConfigurationManager.ConnectionStrings[Constantes.DB_NAME].ConnectionString);

                connection.Open();
                OleDbCommand getUserAvatar = new OleDbCommand("UPDATE Utilisateurs SET Image_Avatar = @ImageName WHERE Username = @username;", connection);
                getUserAvatar.Parameters.Add(new OleDbParameter("ImageName", lblUserName.Text + ImageName) { OleDbType = OleDbType.VarChar, Size = 255 });
                getUserAvatar.Parameters.Add(new OleDbParameter("username", Modules.getConnectedUser()) { OleDbType = OleDbType.VarChar, Size = 255 });
                getUserAvatar.Prepare();

                getUserAvatar.ExecuteNonQuery();
                fuAvatar.SaveAs(Server.MapPath(@"User_Data\images\" + lblUserName.Text + ImageName));
                Session["Image_Avatar"] =@"User_Data\images\" +  lblUserName.Text + ImageName;
                Session["changeAvatarSuccess"] = true;
                connection.Close();
                Server.Transfer("Profile.aspx");
            }
            else
            {
                lblErrors.Text = errorMessages;
            }
        }

        protected string statutUser()
        {
            switch (Modules.isAdmin(Modules.getConnectedUser()))
            {
                case true:
                    if (Modules.isMod(Modules.getConnectedUser()))
                    {
                        return "Administrateur-Modérateur";
                    }
                    else
                    {
                        return "Administrateur";
                    }
                default:
                    if (Modules.isMod(Modules.getConnectedUser()))
                    {
                        return "Utilisateur-Modérateur";
                    }
                    else
                    {
                        return "Utilisateur";
                    }
            }
        }
    }
}